package com.xiaohao.communication.controller;

import com.xiaohao.communication.bean.Answer;
import com.xiaohao.communication.bean.PraiseDetails;
import com.xiaohao.communication.bean.UserTopic;
import com.xiaohao.communication.service.UserTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


@Controller
public class UserTopicController {


    @Autowired
    UserTopicService userTopicService;

    //发布话题      success                 前端不设置date，这里采用系统当前时间
    @ResponseBody
    @GetMapping("/add-topic")
    public void publishTopic(UserTopic userTopic) {
        userTopic.setDate(new Date());      //采用系统当前时间
        userTopicService.publishTopic(userTopic);
    }


    //删除话题      success
    @ResponseBody
    @GetMapping("/delete-topic")
    public void deleteTopic(int id) {
        userTopicService.deleteTopic(id);
    }


    //发布回答      success             前端不设置date，这里采用系统当前时间
    @ResponseBody
    @GetMapping("/add-comment")
    public void publihAnswer(Answer answer) {
        answer.setDate(new Date());     //采用系统当前时间
        userTopicService.publihAnswer(answer);
    }

    //删除回答      success
    @ResponseBody
    @GetMapping("/delete-comment")
    public void deleteAnswer(int id) {
        userTopicService.deleteAnswer(id);
    }


    /***********************************/

    //点赞                        success
    @GetMapping("/add-praise")
    @ResponseBody
    public void addPraise(PraiseDetails praiseDetails) {
        userTopicService.addPraise(praiseDetails);
    }

    //取消赞                       success
    @GetMapping("/delete-praise")
    @ResponseBody
    public void deletePraise(int topic_id) {
        userTopicService.deletePraise(topic_id);
    }

    //模糊查询话题                success
    @GetMapping("/query-topic")
    @ResponseBody
    public List<UserTopic> getUserTopic(String content) {
        List<UserTopic> userTopic = userTopicService.getUserTopic(content);
        return userTopic;
    }


    //获取的当前话题所有回答       success
    @GetMapping("/query-all-comment")
    @ResponseBody
    public List<Answer> getCurrTopicAllAnswer(int topic_id) {
        List<Answer> currTopicAllAnswer = userTopicService.getCurrTopicAllAnswer(topic_id);
        return currTopicAllAnswer;
    }


    //获取所有话题
    @GetMapping("/query-all-Topic")
    @ResponseBody
    public List<UserTopic> getAllUserTopic() {
        List<UserTopic> allUserTopic = userTopicService.getAllUserTopic();
        return allUserTopic;
    }

    //根据话题ID获取话题信息
    @GetMapping("/get-topic-byid")
    @ResponseBody
    public UserTopic getTopicByTopicId(int topic_id) {
        return userTopicService.getTopicByTopicId(topic_id);
    }

}
